# Figure 2

library(tidyverse)

DATA    <- read.csv(file.choose(), header=TRUE) 

theme.z <- theme(
        axis.text.x        = element_text(size=15, color="black", hjust=0.5, margin=margin(t=8,b=8)),
        axis.text.x.top    = element_text(size=15, color="black", hjust=1  , margin=margin(t=8,b=8)),
        axis.text.y        = element_blank(),
        axis.text.y.right  = element_blank(),
        axis.ticks.x       = element_blank(),
        axis.ticks.y       = element_blank(),
        axis.title.x       = element_blank(),
        axis.title.y       = element_blank(),
        legend.position	   = "none",
        legend.text        = element_blank(),
        panel.background   = element_rect(size=0.5, color="black", fill="gray90", linetype="solid"),
        panel.border       = element_rect(size=1.8, color="black", fill=NA      , linetype="solid"),
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(), 
        panel.grid.minor.y = element_blank(),
        plot.background    = element_rect(fill="white"),
        plot.caption       = element_text(size=12 , hjust=0  , margin=margin(t=10)),
        plot.margin        = unit(c(t=5,r=5,b=5,l=5),"pt"),
        plot.subtitle      = element_text(size=15 , hjust=0.5, margin=margin(b=10)),
        plot.title         = element_text(size=20 , hjust=0.5, margin=margin(t=0,b=15), face="bold"),
        strip.background   = element_rect(color="black", fill="black"),
        strip.text.x       = element_text(size=18 , color="white", face="bold"))

ggplot(data=DATA, aes(x=X, y=PE)) +
  geom_col(color="black", fill=c(rep_len("blue3",2), rep_len("gray50",3), rep_len("red3",2)), size=1.1, width=0.8) +
  geom_text(aes(x=X, y=PE, label=scales::percent(abs(PE), accuracy=1L)), size=5, hjust=0.5, vjust=-2) +
  geom_errorbar(aes(ymin=CILO, ymax=CIHI), size=0.75, width=0.25) + 
  scale_x_continuous(expand=c(0,0), limits=c(0.4,7.6), breaks=seq(1,7,1), labels=c("Strong\nDemocrat", "Not strong\nDemocrat", "Independent\nDemocrat", "Independent", "Independent\nRepublican", "Not strong\nRepublican", "Strong\nRepublican")) +
  scale_y_continuous(expand=c(0,0), limits=c(0,1)) +
  theme.z

ggsave(file="Figure 2.wmf", width=10, height=4.5)
getwd()
